package com.atguigu.flink.window;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.function.WaterSensorMapFunction;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class KeyByAndNoKeyBy {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);

        SingleOutputStreamOperator<WaterSensor> mapDS = socketDS.map(new WaterSensorMapFunction());

        KeyedStream<WaterSensor, String> keyByDS = mapDS.keyBy(waterSensor -> waterSensor.getId());

        WindowedStream<WaterSensor, String, TimeWindow> windowDS = keyByDS.window(TumblingProcessingTimeWindows.of(Time.milliseconds(10000)));

        /**
         * 1.增量聚合 来一条处理一条
         * reduce
         * aggregate
         *
         */
        SingleOutputStreamOperator<WaterSensor> reduceDS = windowDS.reduce(
                new ReduceFunction<WaterSensor>() {
                    @Override
                    public WaterSensor reduce(WaterSensor value1, WaterSensor value2) throws Exception {
                        value1.setVc(value1.getVc() + value2.getVc());
                        return value1;
                    }
                }
        );

        SingleOutputStreamOperator<String> aggregateDS = windowDS.aggregate(
                /**
                 *
                 */
                new AggregateFunction<WaterSensor, Integer, String>() {
                    @Override
                    public Integer createAccumulator() {
                        return 0;
                    }

                    @Override
                    public Integer add(WaterSensor waterSensor, Integer integer) {
                        return integer + waterSensor.getVc();
                    }

                    @Override
                    public String getResult(Integer integer) {
                        return "当前窗口水位值和: " + integer;
                    }

                    //只有会话窗口需要实现
                    @Override
                    public Integer merge(Integer integer, Integer acc1) {
                        return 0;
                    }
                }
        );

        /**
         * 全量聚合 将窗口数据进行缓存，等窗口触发计算的时候再进行处理
         * 优点：获取更多窗口信息
         * 缺点：占用空间
         * apply
         *      需要实现WindowFunction<IN, OUT, KEY, W extends Window> ->
         *          实现方法apply(KEY var1, W var2, Iterable<IN> var3, Collector<OUT> var4)
         * process
         *      需要实现ProcessWindowFunction<IN, OUT, KEY, W extends Window> ->
         *          实现方法process(String s, ProcessWindowFunction<WaterSensor, String, String, TimeWindow>.Context context, Iterable<WaterSensor> input, Collector<String> out)
         *
         */

        SingleOutputStreamOperator<String> applyDS = windowDS.apply(
                new WindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void apply(String s, TimeWindow timeWindow, Iterable<WaterSensor> input, Collector<String> out) throws Exception {
                        String start = DateFormatUtils.format(timeWindow.getStart(), "yyyy-MM-dd");
                        String end = DateFormatUtils.format(timeWindow.getEnd(), "yyyy-MM-dd");
                        long count = input.spliterator().estimateSize();
                        out.collect("key=" + s + "的窗口[" + start + "," + end + ")包含" + count + "条数据===>" + input.toString());
                    }
                }
        );

        SingleOutputStreamOperator<String> processDS = windowDS.process(
                new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String s, ProcessWindowFunction<WaterSensor, String, String, TimeWindow>.Context context, Iterable<WaterSensor> input, Collector<String> out) throws Exception {
                        String start = DateFormatUtils.format(context.window().getStart(), "yyyy-MM-dd");
                        String end = DateFormatUtils.format(context.window().getEnd(), "yyyy-MM-dd");
                        long count = input.spliterator().estimateSize();
                        out.collect("key=" + s + "的窗口[" + start + "," + end + ")包含" + count + "条数据===>" + input.toString());
                    }
                }
        );

        reduceDS.print();

        env.execute();

    }
}
